/***disposecmd.cpp
 *	2011.11.4, liyang
 *	Version 1.0
 *	implement send cmd to mem and recv result from mem
 ***/

#include "disposecmd.h"


extern struct StMemory g_memory;				//shared memory
extern struct StRecvCmd g_recvcmd[MAXCMD];		//recv cmd,recv from server send to mem
extern struct StSendMsg g_sendmsg[MAXCMD];		//send msg,recv from mem send to server

extern char g_serialnum[22];					//serianum
extern int g_connectflag;						//0 if socket is outline or 1 connect successfully
extern char g_elecendtime[12];					//endtime for elec
extern char g_powerendtime[12];					//endtime for power

int g_sendflag;									//

/***disposeCmd biref send cmd to mem and recv result from mem ,include safeguard/devlist/electime_cmd
 * 2012.02.22, liyang
 * param void
 *
 * retval void
 * Version 4.0
 ****************************************************************************************
 * liyang	2012.02.22	v4.0	add timetask return
 *								modify inquiryElecTime and inquiryPowerTime
 ***/
void disposeCmd()
{
	int l_temp;											//the number of cmd
	int l_cmd_number;									//sync cmd number
	int l_cmd_temp;
	int l_errtimes;										//err times
	int l_sync_flag;									//devs/modl/time
	int l_sync_head;									//sync cmd head
	int l_sync_tail;									//sync cmd tail
	int l_sync_time;									//sync time
	int l_number_r;										//recv number
	int l_number_s;										//send number
	int l_strlen;										//str len
	long int l_etime;									//inquiry elec time
	long int l_wtime;									//inquiry power time
	char l_cmd[4];										//save "#re" or "#tt"
	char l_str[40];										//str:ip and port from server
	struct timeval l_tv;								//parameter of gettimeofday

	l_strlen=0;
	l_number_r=0;
	l_number_s=0;
	l_sync_flag=0;
	g_sendflag=0;

	//get inquiryelectime
	l_etime=inquiryElecTime();
	//get inquirypowertime
	l_wtime=inquiryPowerTime();

	while(1)
	{
		BEGIN:
		//set time?
		if (shm_TIME == g_memory.shmADDR[0])
		{
			g_memory.shmADDR[0]=shm_CLEAR;											//set mem flag: unlock
			if(-1==gettimeofday(&l_tv,NULL))
			{
				ERROROUT("get-time failed!");
			}
			/*20120222; modify inquiryElecTime and inquiryPowerTime; Start*/
			//get inquiryelectime
			if ( l_etime < l_tv.tv_sec || (l_etime-l_tv.tv_sec >= 3960))
				l_etime=inquiryElecTime();
			//get inquirypowertime
			if ( l_wtime < l_tv.tv_sec || (l_wtime-l_tv.tv_sec >= 3960))
				l_wtime=inquiryPowerTime();
			/*20120222; End modify inquiryElecTime and inquiryPowerTime; End*/
		}
		//recv_cmd need write into mem
		for (int i=0;i<MAXCMD;i++)
		{
			if (true == g_recvcmd[l_number_r].flag)
			{
				//'tt' return 'cc'
				l_cmd[0]='#';
				l_cmd[1]='c';
				l_cmd[2]='c';
				l_cmd[3]='\0';
				//save str
				l_temp=4;
				while('#' != g_recvcmd[l_number_r].cmd[l_temp])
					l_temp++;
				l_strlen=l_temp-4;
				memcpy(&l_str[0],&g_recvcmd[l_number_r].cmd[4],l_strlen);
				l_str[l_strlen]='\0';

			//save sync cmd
				if ('s' == g_recvcmd[l_number_r].cmd[5+l_strlen] && 'y' == g_recvcmd[l_number_r].cmd[6+l_strlen] && 'n' == g_recvcmd[l_number_r].cmd[7+l_strlen] && 'c' == g_recvcmd[l_number_r].cmd[8+l_strlen])
				{
					if ('d' == g_recvcmd[l_number_r].cmd[10+l_strlen])
					{
						//memory flag set
						if (shm_CLEAR != g_memory.shmADDR[0])									//write mem
							break;
						g_memory.shmADDR[0]=shm_UPGRADE;
						while(shm_UPRESPOND != g_memory.shmADDR[0])
							myDelay(1);
						l_sync_flag=1;
						PRINTOUT("%s","sync devs!\n");
					}
					if ('m' == g_recvcmd[l_number_r].cmd[10+l_strlen])
					{
						//if memory flag is
						if (shm_UPRESPOND == g_memory.shmADDR[0] && 1 == l_sync_flag)
						{
							l_sync_flag=2;
							PRINTOUT("%s","sync modl!\n");
						}
						else
						{
							for (int i=0;i<MAXCMD;i++)
								g_recvcmd[i].flag=false;
							l_sync_flag=0;
							saveSyncResultIntoSendmsg(l_number_s,l_cmd,l_strlen,l_str,0);
							break;
						}
					}
					if ('t' == g_recvcmd[l_number_r].cmd[10+l_strlen])
					{
						//if memory flag is
						if (shm_UPRESPOND == g_memory.shmADDR[0] && 2 == l_sync_flag)
						{
							l_sync_flag=3;
							PRINTOUT("%s","sync modl!\n");
						}
						else
						{
							for (int i=0;i<MAXCMD;i++)
								g_recvcmd[i].flag=false;
							l_sync_flag=0;
							saveSyncResultIntoSendmsg(l_number_s,l_cmd,l_strlen,l_str,0);
							break;
						}
					}

					//sync cmd number
					l_temp=14+l_strlen;
					l_cmd_temp=0;
					while('\0' != g_recvcmd[l_number_r].cmd[l_temp])
					{
						if ('#' == g_recvcmd[l_number_r].cmd[l_temp])
							l_cmd_temp++;
						l_temp++;
					}
					l_cmd_number=(l_cmd_temp-1)/5;
					if (1 == l_sync_flag)
						PRINTOUT("devs :%d cmds synchronization\n",l_cmd_number);
					if (2 == l_sync_flag)
						PRINTOUT("modl :%d cmds synchronization\n",l_cmd_number);
					if (3 == l_sync_flag)
						PRINTOUT("time :%d cmds synchronization\n",l_cmd_number);

					//send sync cmd to memory
					l_sync_head=14+l_strlen;
					l_sync_tail=l_sync_head;
					l_errtimes=3;
					while(0 != l_cmd_number)
					{
						l_cmd_temp=6;
						while(0 != l_cmd_temp)
						{
							if ('#' == g_recvcmd[l_number_r].cmd[l_sync_tail])
							{
								l_cmd_temp--;
								l_sync_tail++;
							}
							else
								l_sync_tail++;
						}
						memcpy(&g_memory.shmADDR[1],&g_recvcmd[l_number_r].cmd[l_sync_head],l_sync_tail-l_sync_head);
						g_memory.shmADDR[l_sync_tail-l_sync_head+1]='\0';
						g_memory.shmADDR[0]=shm_CMD;
						PRINTOUT("mem:%s\n",&g_memory.shmADDR[1]);

						//recv result
						if (-1 == waitSyncResultFromCC())
						{
							if (1 == l_sync_flag)
								PRINTOUT("devs :sync cmd failed,try %d times\n",l_errtimes-1);
							if (2 == l_sync_flag)
								PRINTOUT("modl :sync cmd failed,try %d times\n",l_errtimes-1);
							if (3 == l_sync_flag)
								PRINTOUT("time :sync cmd failed,try %d times\n",l_errtimes-1);
							l_errtimes--;
							l_sync_tail=l_sync_head;
							if (0 == l_errtimes)
							{
								if (1 == l_sync_flag)
									PRINTOUT("%s","devs :sync failed,don't try again!!!!!\n");
								if (2 == l_sync_flag)
									PRINTOUT("%s","modl :sync failed,don't try again!!!!!\n");
								if (3 == l_sync_flag)
									PRINTOUT("%s","time :sync failed,don't try again!!!!!\n");
								for (int i=0;i<MAXCMD;i++)
									g_recvcmd[i].flag=false;
								//send sync ff
								l_sync_flag=0;
								saveSyncResultIntoSendmsg(l_number_s,l_cmd,l_strlen,l_str,0);
								goto BEGIN;
							}
						}
						else
						{
							l_errtimes=3;
							l_cmd_number--;
							l_sync_head=l_sync_tail-1;
							l_sync_tail=l_sync_head;
							if (1 == l_sync_flag)
								PRINTOUT("devs :%d cmds synchronization\n",l_cmd_number);
							if (2 == l_sync_flag)
								PRINTOUT("modl :%d cmds synchronization\n",l_cmd_number);
							if (3 == l_sync_flag)
								PRINTOUT("time :%d cmds synchronization\n",l_cmd_number);
						}
					}
					g_recvcmd[l_number_r].flag=false;
					if (MAXCMD == (l_number_r+1))
						l_number_r=0;
					else
						l_number_r++;
					if (1 == l_sync_flag)
					{
						PRINTOUT("%s","devs :sync ok!\n");
						//set time: if next cmd not come
						if(-1==gettimeofday(&l_tv,NULL))
						{
							ERROROUT("get-time failed!");
						}
						l_sync_time=l_tv.tv_sec;
					}
					if (2 == l_sync_flag)
					{
						PRINTOUT("%s","modl :sync ok!\n");
						//set time: if next cmd not come
						if(-1==gettimeofday(&l_tv,NULL))
						{
							ERROROUT("get-time failed!");
						}
						l_sync_time=l_tv.tv_sec;
					}
					if (3 == l_sync_flag)
					{
						PRINTOUT("%s","time :sync ok!\n");
						l_sync_flag=0;
						saveSyncResultIntoSendmsg(l_number_s,l_cmd,l_strlen,l_str,1);
					}
				}

			//save normal cmd
				else								//cmd need write into mem
				{
					if (shm_CLEAR != g_memory.shmADDR[0])									//write mem
						break;
					//write mem
					g_memory.shmADDR[0]=shm_SWRITED;										//set mem flag: lock
					if (shm_WRITED == g_memory.shmADDR[0])
						break;
					myDelay(5);																//delay 5ms
					//save normal cmd
					l_temp=0;
					while('\0' != g_recvcmd[l_number_r].cmd[l_temp])
						l_temp++;
					memcpy(&g_memory.shmADDR[1],&g_recvcmd[l_number_r].cmd[4+l_strlen],l_temp-4-l_strlen);	//copy cmd to mem
					g_memory.shmADDR[l_temp-3-l_strlen]='\0';
					g_recvcmd[l_number_r].flag=false;
					if (MAXCMD == (l_number_r+1))
						l_number_r=0;
					else
						l_number_r++;
					//set mem falg
					g_memory.shmADDR[0]=shm_CMD;										//set mem flag: dispose
					//wait result
					waitResultFromCC(l_number_s,l_cmd,l_strlen,l_str,0);
					break;
				}
			}
			//save no cmd
			else
			{
				if (0 != l_sync_flag)
				{
					if(-1==gettimeofday(&l_tv,NULL))
					{
						ERROROUT("get-time failed!");
					}
					if (SYNCTIME < (l_tv.tv_sec-l_sync_time) )
					{
						l_sync_flag=0;
						saveSyncResultIntoSendmsg(l_number_s,l_cmd,l_strlen,l_str,0);
					}
				}
				if (MAXCMD == (l_number_r+1))
					l_number_r=0;
				else
					l_number_r++;
			}
		}
		//safeguard_cmd need write into mem
		if (shm_ALARM == g_memory.shmADDR[0])
		{
			l_cmd[0]='#';
			l_cmd[1]='a';
			l_cmd[2]='m';
			l_cmd[3]='\0';
			waitResultFromCC(l_number_s,l_cmd,0,NULL,1);
		}

		//inquiry power_cmd need write into mem
		if(-1==gettimeofday(&l_tv,NULL))
		{
			ERROROUT("get-time failed!");
		}
		if (1 == g_connectflag && l_tv.tv_sec > l_wtime)
		{
			if (shm_CLEAR != g_memory.shmADDR[0])							//write mem
				continue;
			//write mem
			g_memory.shmADDR[0]=shm_SWRITED;								//set mem flag: lock
			myDelay(5);														//delay 5ms
			if (shm_WRITED == g_memory.shmADDR[0])
				continue;
			//send to mem
			memcpy(&g_memory.shmADDR[1],"#0000000000#02#0201#03#",23);
			memcpy(&g_memory.shmADDR[24],&g_powerendtime[0],10);
			memcpy(&g_memory.shmADDR[34],"0000000000",10);
			g_memory.shmADDR[44]='#';
			g_memory.shmADDR[45]='\0';

			//wait result
			l_cmd[0]='#';
			l_cmd[1]='r';
			l_cmd[2]='e';
			l_cmd[3]='\0';
		    //set mem falg
		    g_memory.shmADDR[0]=shm_CMD;										//set mem flag: dispose
			waitResultFromCC(l_number_s,l_cmd,0,NULL,0);

			//get inquirypowertime
			if ( 0 == g_sendflag)
				l_wtime=inquiryPowerTime();
			else if ( 1 == g_sendflag)
			{
				if(-1==gettimeofday(&l_tv,NULL))
				{
					ERROROUT("get-time failed!");
				}
				l_wtime=l_tv.tv_sec+20;
			}
			g_sendflag=0;
		}

		//inquiry_elec_cmd need write into mem
		if(-1==gettimeofday(&l_tv,NULL))
		{
			ERROROUT("get-time failed!");
		}
		if (1 == g_connectflag && l_tv.tv_sec > l_etime)
		{
			if (shm_CLEAR != g_memory.shmADDR[0])							//write mem
				continue;
			//write mem
			g_memory.shmADDR[0]=shm_SWRITED;								//set mem flag: lock
			myDelay(5);														//delay 5ms
			if (shm_WRITED == g_memory.shmADDR[0])
				continue;
			//send to mem
			memcpy(&g_memory.shmADDR[1],"#0000000000#02#0301#04#",23);
			memcpy(&g_memory.shmADDR[24],&g_elecendtime[0],10);
			memcpy(&g_memory.shmADDR[34],"0000000000",10);
			g_memory.shmADDR[44]='#';
			g_memory.shmADDR[45]='\0';

			//wait result
			l_cmd[0]='#';
			l_cmd[1]='r';
			l_cmd[2]='e';
			l_cmd[3]='\0';
		    //set mem falg
		    g_memory.shmADDR[0]=shm_CMD;										//set mem flag: dispose
			waitResultFromCC(l_number_s,l_cmd,0,NULL,0);

			//get inquiryelectime
			if ( 0 == g_sendflag)
				l_etime=inquiryElecTime();
			else if ( 1 == g_sendflag)
			{
				if(-1==gettimeofday(&l_tv,NULL))
				{
					ERROROUT("get-time failed!");
				}
				l_etime=l_tv.tv_sec+20;
			}
			g_sendflag=0;
		}

		/*2012.02.22; add timetask return ;Start*/
		//timetask return
		if (shm_STATUS == g_memory.shmADDR[0])
		{
			g_memory.shmADDR[0]=shm_SWRITED;								//set mem flag: lock

			//send to mem
			memcpy(&g_memory.shmADDR[1],"#0000000000#02#0101#00#",23);
			g_memory.shmADDR[24]='\0';
			//wait result
			l_cmd[0]='#';
			l_cmd[1]='r';
			l_cmd[2]='e';
			l_cmd[3]='\0';
			//set mem falg
			g_memory.shmADDR[0]=shm_CMD;										//set mem flag: dispose
			waitResultFromCC(l_number_s,l_cmd,0,NULL,0);
		}
		/*2012.02.22; add timetask return ;End*/

		myDelay(5);
	}
}

/***waitSyncResultFromCC biref wait sync result from CC
 * 2012.1.11, liyang
 * param void
 *
 * retval 0 if successful or -1 failed
 * Version 1.0
 ***/
int waitSyncResultFromCC(void)
{
	int l_temp=0;

	while (shm_RESULT != g_memory.shmADDR[0])
	{
		myDelay(5);
		if(l_temp++>WAITRES)
		{
			g_memory.shmADDR[0]=shm_UPRESPOND;
			return -1;
		}
	}

	if ( ('0' == g_memory.shmADDR[25] && '0' == g_memory.shmADDR[26]) || ('f' == g_memory.shmADDR[25] && '2' == g_memory.shmADDR[26]))
	{
		g_memory.shmADDR[0]=shm_UPRESPOND;
		return 0;
	}
	else
	{
		g_memory.shmADDR[0]=shm_UPRESPOND;
		return -1;
	}
}

/***saveSyncResultIntoSendmsg biref save sync result into g_sendmsg
 * 2012.1.11, liyang
 * param l_number_s[in][out], msg number
 * param l_cmd[in], "#re" or "#tt"
 * param l_strlen[in], len of str
 * param l_str[in], ip and port from server
 * param l_alarm_flag[in], alarm flag
 *
 * retval void
 * Version 1.0
 ***/
void saveSyncResultIntoSendmsg(int &l_number_s,char *l_cmd,int l_strlen,char *l_str,int l_result)
{
	int l_temp;
	for (int i=0;i<MAXCMD;i++)
	{
		if (false == g_sendmsg[l_number_s].flag)
		{
			while('\0' != g_memory.shmADDR[l_temp])
				l_temp++;
			//#cc#
			g_sendmsg[l_number_s].msg[0]=l_cmd[0];
			g_sendmsg[l_number_s].msg[1]=l_cmd[1];
			g_sendmsg[l_number_s].msg[2]=l_cmd[2];
			g_sendmsg[l_number_s].msg[3]='#';
			//#cc#str#serialnum#
			memcpy(&g_sendmsg[l_number_s].msg[4],l_str,l_strlen);
			g_sendmsg[l_number_s].msg[4+l_strlen]='#';
			memcpy(&g_sendmsg[l_number_s].msg[5+l_strlen],g_serialnum,20);
			g_sendmsg[l_number_s].msg[25+l_strlen]='#';
			//#cc#str#serialnum#sync#ff/ok#
			g_sendmsg[l_number_s].msg[26+l_strlen]='s';
			g_sendmsg[l_number_s].msg[27+l_strlen]='y';
			g_sendmsg[l_number_s].msg[28+l_strlen]='n';
			g_sendmsg[l_number_s].msg[29+l_strlen]='c';
			g_sendmsg[l_number_s].msg[30+l_strlen]='#';
			if (0 == l_result)
			{
				g_sendmsg[l_number_s].msg[31+l_strlen]='f';
				g_sendmsg[l_number_s].msg[32+l_strlen]='f';
			}
			else if (1 == l_result)
			{
				g_sendmsg[l_number_s].msg[31+l_strlen]='o';
				g_sendmsg[l_number_s].msg[32+l_strlen]='k';
			}
			g_sendmsg[l_number_s].msg[33+l_strlen]='#';
			g_sendmsg[l_number_s].msg[34+l_strlen]='\0';

			g_sendmsg[l_number_s].flag=true;
			if (MAXCMD == (l_number_s)+1)
				l_number_s=0;
			else
				(l_number_s)++;
			break;
		}
		else
		{
			if (MAXCMD == (l_number_s)+1)
				l_number_s=0;
			else
				(l_number_s)++;
		}
	}
	g_memory.shmADDR[0]=shm_CLEAR;
}

/***waitResultFromCC biref wait result from CC and save it into g_sendmsg
 * 2011.12.30, liyang
 * param l_number_s[in][out], msg number
 * param l_cmd[in], "#re" or "#tt"
 * param l_strlen[in], len of str
 * param l_str[in], ip and port from server
 * param l_alarm_flag[in], alarm flag
 *
 * retval void
 * Version 3.0
 ***/
void waitResultFromCC(int &l_number_s,char *l_cmd,int l_strlen,char *l_str,int l_alarm_flag)
{
	int l_temp=0;
	LOGI("[waitResultFromCC] l_cmd =%s",l_cmd);
	LOGI("[waitResultFromCC] l_cmd =%s",l_str);
	if (1 == l_alarm_flag)
		goto GET;

	while (shm_RESULT != g_memory.shmADDR[0])
	{
		myDelay(5);
		if(l_temp++>WAITRES)
		{
			if (false == g_sendmsg[l_number_s].flag)
			{
				g_sendmsg[l_number_s].msg[0]=l_cmd[0];
				g_sendmsg[l_number_s].msg[1]=l_cmd[1];
				g_sendmsg[l_number_s].msg[2]=l_cmd[2];
				g_sendmsg[l_number_s].msg[3]='#';
				if (0 == l_strlen)
				{
					memcpy(&g_sendmsg[l_number_s].msg[4],g_serialnum,20);
					memcpy(&g_sendmsg[l_number_s].msg[24],"#ffffffffff#ff#ffff#ff#",23);
					g_sendmsg[l_number_s].msg[47]='\0';
				}
				else
				{
					memcpy(&g_sendmsg[l_number_s].msg[4],l_str,l_strlen);
					g_sendmsg[l_number_s].msg[4+l_strlen]='#';
					memcpy(&g_sendmsg[l_number_s].msg[5+l_strlen],g_serialnum,20);
					memcpy(&g_sendmsg[l_number_s].msg[25+l_strlen],"#ffffffffff#ff#ffff#ff#",23);
					g_sendmsg[l_number_s].msg[48+l_strlen]='\0';
				}
				g_sendmsg[l_number_s].flag=true;
				if (MAXCMD == (l_number_s)+1)
					l_number_s=0;
				else
					(l_number_s)++;
			}
			goto END;
		}
	}

	GET:
	for (int i=0;i<MAXCMD;i++)
	{
		if (false == g_sendmsg[l_number_s].flag)
		{
			while('\0' != g_memory.shmADDR[l_temp])
				l_temp++;
			g_sendmsg[l_number_s].msg[0]=l_cmd[0];
			g_sendmsg[l_number_s].msg[1]=l_cmd[1];
			g_sendmsg[l_number_s].msg[2]=l_cmd[2];
			g_sendmsg[l_number_s].msg[3]='#';
			if (0 == l_strlen)
			{
				if ( 'r' == l_cmd[1] && 'e' == l_cmd[2])
				{
					//save elec endtime
					if ('2' == g_memory.shmADDR[14] && '3' == g_memory.shmADDR[17] && '1' == g_memory.shmADDR[19] && '4' == g_memory.shmADDR[22])
					{
						if ('1' == g_memory.shmADDR[l_temp-2])
							g_sendflag=1;
						else
							g_sendflag=0;
						memcpy(&g_elecendtime[0],&g_memory.shmADDR[l_temp-14],10);
						g_elecendtime[10]='\0';
						l_temp=l_temp-14;
					}
					//save power endtime
					if ('2' == g_memory.shmADDR[14] && '2' == g_memory.shmADDR[17] && '1' == g_memory.shmADDR[19] && '3' == g_memory.shmADDR[22])
					{
						if ('1' == g_memory.shmADDR[l_temp-2])
							g_sendflag=1;
						else
							g_sendflag=0;
						memcpy(&g_powerendtime[0],&g_memory.shmADDR[l_temp-14],10);
						g_powerendtime[10]='\0';
						l_temp=l_temp-14;
					}
				}
				memcpy(&g_sendmsg[l_number_s].msg[4],g_serialnum,20);
				memcpy(&g_sendmsg[l_number_s].msg[24],&g_memory.shmADDR[1],l_temp-1);
				g_sendmsg[l_number_s].msg[l_temp+23]='\0';
			}
			else
			{
				memcpy(&g_sendmsg[l_number_s].msg[4],l_str,l_strlen);
				g_sendmsg[l_number_s].msg[4+l_strlen]='#';
				memcpy(&g_sendmsg[l_number_s].msg[5+l_strlen],g_serialnum,20);
				memcpy(&g_sendmsg[l_number_s].msg[25+l_strlen],&g_memory.shmADDR[1],l_temp-1);
				g_sendmsg[l_number_s].msg[l_strlen+l_temp+24]='\0';
			}
			g_sendmsg[l_number_s].flag=true;
			if (MAXCMD == (l_number_s)+1)
				l_number_s=0;
			else
				(l_number_s)++;
			break;
		}
		else
		{
			if (MAXCMD == (l_number_s)+1)
				l_number_s=0;
			else
				(l_number_s)++;
		}
	}
	END:
	g_memory.shmADDR[0]=shm_CLEAR;				//set mem flag: unlock
}

/***inquiryPower Time biref inquiry power_time
 * 2011.11.8, liyang
 * param void
 *
 * retval, l_wtime(inquiry power time) if successful or -1 failed
 * Version 1.0
 ***/
long int inquiryPowerTime()
{
	long int l_time;						//05 minutes every hour
	long int l_wtime;						//inquiry power time
	struct timeval l_tv;					//parameter of gettimeofday

	//get time
	if(-1==gettimeofday(&l_tv,NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}

	l_time=(l_tv.tv_sec/3600)*3600+3300;
	if (l_tv.tv_sec > l_time)
		l_wtime=l_time+random()%120+3600+360;
	else
		l_wtime=l_time+random()%120+360;

	PRINTOUT("power inquiry at:%ld\n",l_wtime);
	return l_wtime;
}

/***inquiryElecTime biref inquiry elec_time
 * 2011.11.8, liyang
 * param void
 *
 * retval, l_etime(inquiry elec time) if successful or -1 failed
 * Version 1.0
 ***/
long int inquiryElecTime()
{
	long int l_time;						//50 minutes every hour
	long int l_etime;						//inquiry elec time
	struct timeval l_tv;					//parameter of gettimeofday

	//get time
	if(-1==gettimeofday(&l_tv,NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}

	l_time=(l_tv.tv_sec/3600)*3600+3000;
	if (l_tv.tv_sec > l_time)
		l_etime=l_time+random()%120+3600+360;
	else
		l_etime=l_time+random()%120+360;

	PRINTOUT("elec inquiry at:%ld\n",l_etime);
	return l_etime;
}

/***myDelay() brief implement delay function.
 *  2011.11.8, liyang
 *	param 	delay[in], 'ms' to delay.
 *
 *	retval  void
 *	retval  Version 1.0
 ***/
void myDelay(int delay)
{
	if(delay<=0)
		return;
	struct timeval timeout;
	timeout.tv_sec=delay/1000;
	timeout.tv_usec=(delay%1000)*1000;			///ms
	select(0,NULL,NULL,NULL,&timeout);
}
